---
title: "multicorHeatmap"
author: "果仁菌"
date: "8/11/2020"
output: pdf_document
---

```{r packages}
library(vegan)
library(ggcor)
library(ggplot2)
```

## 读入事例文件

```{r read_tableA}
tableA_spec <- read.csv("www/Spec.csv",row.names = 1)
```

```{r read_tableB}
tableB_env  <- read.csv("www/Env.csv", row.names = 1)
```

```{r tri_reslut}
tri_result <- fortify_cor(tableB_env,
                          type = "upper",
                          show.diag = F,
                          cor.test = T)
```

## 绘制三角热图

```{r plot1}
quickcor(tri_result,
         axis.label.drop = F,
         grid.colour = "black",
         grid.size = 0.4,
         fixed.xy = T) + 
  geom_square()
```

```{r plot2}
quickcor(tri_result,
         axis.label.drop = F,
         grid.colour = "red",
         grid.size = 0.4,
         fixed.xy = T) + 
  geom_square()
```

```{r plot3}
quickcor(tri_result,
         axis.label.drop = F,
         grid.colour = "red",
         grid.size = 1,
         fixed.xy = T) + 
  geom_square()
```

```{r plot4}
quickcor(tri_result,
         axis.label.drop = F,
         grid.colour = "red",
         grid.size = 0.1,
         fixed.xy = T) + 
  geom_pie2()
```

```{r plot5}
quickcor(tri_result,
         axis.label.drop = F,
         grid.colour = "black",
         grid.size = 0.1,
         fixed.xy = T) + 
  geom_square() +
  scale_fill_gradient2n()
```

## 加入连线

```{r connect_result}
# 三角部分
tri_result <- fortify_cor(tableB_env,
                          type = "upper", 
                          show.diag = F,
                          cor.test = TRUE, 
                          cluster.type = "all")

# spec 全表与 env变量子表 分别算mantel's r的结果
connect_result <- mantel_test(tableA_spec,
                              tableB_env,
                              spec.dist.method = "bray",
                              env.dist.method  = "euclidean")
```

```{r plot6}
# 仅三角矩阵部分
quickcor(tri_result,
         axis.label.drop = F,
         grid.colour = "black",
         grid.size = 0.1,
         fixed.xy = T) + 
  geom_square() +
  scale_fill_gradient2n()
```


```{r plot7}
# 加入连线部分
quickcor(tri_result,
         axis.label.drop = F,
         grid.colour = "black",
         grid.size = 0.1,
         fixed.xy = T) + 
  geom_square() +
  scale_fill_gradient2n() +
  anno_link(connect_result,
            aes(color = p.value,
                size  = abs(r)),
            label.size = 4,
            label.colour = "black")
```

```{r plot8}
quickcor(tri_result,
         axis.label.drop = F,
         grid.colour = "black",
         grid.size = 0.1,
         fixed.xy = T) + 
  geom_square() +
  scale_fill_gradient2n() +
  anno_link(connect_result,
            aes(color = p.value,
                size  = abs(r)),
            label.size = 4,
            label.colour = "red")
```

```{r plot9}
quickcor(tri_result,
         axis.label.drop = F,
         grid.colour = "black",
         grid.size = 0.1,
         fixed.xy = T) + 
  geom_square() +
  scale_fill_gradient2n() +
  anno_link(connect_result,
            aes(color = p.value,
                size  = abs(r)),
            label.size = 10,
            label.colour = "red")
```

```{r plot10}
quickcor(tri_result,
         axis.label.drop = F,
         grid.colour = "black",
         grid.size = 0.1,
         fixed.xy = T) + 
  geom_square() +
  scale_fill_gradient2n() +
  anno_link(connect_result,
            aes(color = p.value,
                size  = abs(r)),
            label.size = 10,
            label.colour = "red") +
  scale_color_viridis_c()
```